
#!/bin/bash

read -p "安装go环境是否继续？[y/n]" choice
if [ "$choice" != "y" ]; then
  echo "安装已取消"
  exit 0
fi

echo "开始下载 Go 1.18"
wget https://dl.google.com/go/go1.18.linux-amd64.tar.gz
echo "Go 下载完成"

echo "开始解压 Go 1.18"
sudo tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz
echo "Go 解压完成"

echo "开始更新环境变量"
sudo sh -c 'echo "export PATH=\$PATH:/usr/local/go/bin" >> /etc/profile'
source /etc/profile
source ~/.bashrc   # 加载 .bashrc 文件中的环境变量
echo "环境变量更新成功"
source /etc/profile

echo "开始测试安装"
if [ "$(go version | awk '{print $3}')" = "go1.18" ]; then
  echo "安装成功"
else
  echo "安装失败"
  exit 1
fi